package com.ruoyi.web.controller.system;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.Ti;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ITiService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Skzh;
import com.ruoyi.system.service.ISkzhService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收款账号管理Controller
 *
 * @author ruoyi
 * @date 2025-04-23
 */
@RestController
@RequestMapping("/system/skzh")
public class SkzhController extends BaseController
{
    @Autowired
    private ISkzhService skzhService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ITiService tiService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询收款账号管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:skzh:list')")
    @GetMapping("/list")
    public TableDataInfo list(Skzh skzh)
    {
        SysUser sysUser = getLoginUser().getUser();
        boolean contains3OR4 = sysUser.getRoles().stream().anyMatch(v->v.getRoleId()==3L || v.getRoleId()==4L);
        if(contains3OR4){
            SysDept sysDept = sysDeptService.selectDeptById(getDeptId());
            skzh.setDeptId(sysDept.getDeptId());
        }
        startPage();
        List<Skzh> list = skzhService.selectSkzhList(skzh);
        return getDataTable(list);
    }

    /**
     * 导出收款账号管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:skzh:export')")
    @Log(title = "收款账号管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Skzh skzh)
    {
        List<Skzh> list = skzhService.selectSkzhList(skzh);
        ExcelUtil<Skzh> util = new ExcelUtil<Skzh>(Skzh.class);
        util.exportExcel(response, list, "收款账号管理数据");
    }

    /**
     * 获取收款账号管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:skzh:query')")
    @GetMapping(value = "/{skzhId}")
    public AjaxResult getInfo(@PathVariable("skzhId") Long skzhId)
    {
        return success(skzhService.selectSkzhBySkzhId(skzhId));
    }

    /**
     * 新增收款账号管理
     */
    @PreAuthorize("@ss.hasPermi('system:skzh:add')")
    @Log(title = "收款账号管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Skzh skzh)
    {
        SysUser sysUser = getLoginUser().getUser();
        boolean contains3OR4 = sysUser.getRoles().stream().anyMatch(v->v.getRoleId()==3L || v.getRoleId()==4L);
        if(contains3OR4){
            SysDept sysDept = sysDeptService.selectDeptById(getDeptId());
            skzh.setDeptId(sysDept.getDeptId());
            skzh.setReviewStatus("1");
        }
        LambdaQueryWrapper<Skzh> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Skzh::getSkzhId);
        lambdaQueryWrapper.eq(Skzh::getDeptId,skzh.getDeptId());
        Skzh dbSkzh = skzhService.getOne(lambdaQueryWrapper);
        if(dbSkzh!=null){
            throw new RuntimeException("团队已有账号存在");
        }
        return toAjax(skzhService.insertSkzh(skzh));
    }

    /**
     * 修改收款账号管理
     */
    @PreAuthorize("@ss.hasPermi('system:skzh:editstatus')")
    @Log(title = "收款账号管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeReviewStatus")
    public AjaxResult changeReviewStatus(@RequestBody Skzh skzh)
    {
        skzh.setReviewStatus("0");
        return toAjax(skzhService.updateSkzh(skzh));
    }


    @PreAuthorize("@ss.hasPermi('system:skzh:edit')")
    @Log(title = "收款账号管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Skzh skzh)
    {
        SysUser sysUser = getLoginUser().getUser();
        boolean contains3OR4 = sysUser.getRoles().stream().anyMatch(v->v.getRoleId()==3L || v.getRoleId()==4L);
        if(contains3OR4){
            SysDept sysDept = sysDeptService.selectDeptById(getDeptId());
            skzh.setDeptId(sysDept.getDeptId());
            skzh.setReviewStatus("1");
        }
        skzh.setReviewType("编辑收款账号");
        LambdaQueryWrapper<Skzh> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Skzh::getSkzhId,Skzh::getWithdrawalStatus);
        lambdaQueryWrapper.eq(Skzh::getDeptId,skzh.getDeptId());
        Skzh dbSkzh = skzhService.getOne(lambdaQueryWrapper);
        if(dbSkzh!=null && !dbSkzh.getSkzhId().equals(skzh.getSkzhId())){
            throw new RuntimeException("团队已有账号存在");
        }
        if(dbSkzh!=null && dbSkzh.getWithdrawalStatus().equals("2")){
            throw new RuntimeException("账号提现中不可编辑");
        }
        return toAjax(skzhService.updateSkzh(skzh));
    }

    /**
     * 删除收款账号管理
     */
    @PreAuthorize("@ss.hasPermi('system:skzh:remove')")
    @Log(title = "收款账号管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{skzhIds}")
    public AjaxResult remove(@PathVariable Long[] skzhIds)
    {
        LambdaQueryWrapper<Ti> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.select(Ti::getSkzhId);
        lambdaQueryWrapper.in(Ti::getSkzhId, skzhIds);
        lambdaQueryWrapper.last("limit 1");
        Ti ti = tiService.getOne(lambdaQueryWrapper);
        if(ti!=null){
            throw new RuntimeException("已有账号正处于使用中不可删除");
        }
        return toAjax(skzhService.deleteSkzhBySkzhIds(skzhIds));
    }

    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPermi('system:skzh:list')")
    @GetMapping("/deptTree")
    public AjaxResult deptTree(SysDept dept)
    {
        return success(deptService.selectDeptTreeList(dept));
    }
}
